<?php
/**
 * 通用模块
 */

require_once (API_ROOT_PATH . '/../inc/Config.php');
require_once ('MysqliDb.php');

/**
 * 获取数据库实例
 * @return MysqliDb
 */
function get_mysqli_db ()
{
    $dbLink = new MysqliDb([
        'host' => MYSQLDB,
        'username' => MYSQLDB_USER,
        'password' => MYSQLDB_PWD,
        'db' => MYSQLDB_DATABASE,
        'port' => MYSQLDB_PORT,
        'prefix' => MYSQLDB_PREFIX,
    ]);
    return $dbLink;
}

/**
 * 获取CLCW数据库实例
 * @return MysqliDb
 */
function get_clcw_db ()
{
    $clcwDb = new MysqliDb ([
        'host'     => DB_CLCW_HOST,
        'username' => DB_CLCW_USER,
        'password' => DB_CLCW_PWD,
        'db'       => DB_CLCW_NAME,
        'port'     => DB_CLCW_PORT,
        'prefix'   => DB_CLCW_PREFIX,
        'charset'  => DB_CLCW_CHARSET
    ]);
    return $clcwDb;
}

/**
 * 日志记录方法
 * @param $msg
 * @param string $level DEBUG,INFO,WARN,ERR
 * @return bool
 */
function logUtil($msg,$level="INFO"){

    $cost_mem = memory_usage();
    $log = "[{$level}][".date('Y-m-d H:i:s')."][{$cost_mem}]\t{$msg}\n";
    echo $log;
    unset($log);
}

/**
 * 获取内存使用
 * @return string
 */
function memory_usage() {
    $memory     = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB';
    return $memory;
}